import axios from 'axios'
import  store  from '../store';
const service = axios.create({
    baseURL: 'http://crossentropy.asia/ping',
    timeout: 5000, 
})

service.interceptors.request.use(
    // axios请求拦截器
    (config) => {
        // 判断 有没有token   apikey 
        const { profile } =store.state.user
        if (profile.cookies){
            config.headers.Authorization = 'Bearer ${profile.cookies}' ;   
        }
        return config
    },
    (error) => {
        console.log(error)
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    (res) => res.data,
    (error) => {
        console.log('err' + error);
        // 网络原因
        return Promise.reject('网络原因')
    }
)
//then catch
//get  查询 post 插入db   put 修改  delete  shanc

export default (url, method,submitData)=> {
    return service({
        url,
        [method==='get'?'params':'data']:submitData,
        submitData,
    });
};
